<?php

namespace app\admin\validate\cms;

use app\common\model\article\Article;
use tmcore\base\BaseValidate;

/**
 * 文章管理验证器
 */
class ArticleValidate extends BaseValidate
{
    protected $rule = [
        'id' => 'require|checkArticle',
        'title' => 'require|length:1,100',
        'image' => 'require',
        'cate_id' => 'require',
        'status' => 'require|in:0,1',
        'sort' => 'egt:0',
    ];

    protected $message = [
        'id.require' => '文章id不能为空',
        'title.require' => '标题不能为空',
        'title.length' => '标题长度须在1-100位字符',
        'image.require' => '文章封面不能为空',
        'cate_id.require' => '栏目不能为空',
        'status.require' => '状态不能为空',
        'status.in' => '状态参数有误',
        'sort.egt' => '排序值不正确',
    ];

    /**
     * 添加场景
     * @return ArticleValidate
     */
    public function sceneAdd()
    {
        return $this->remove(['id'])
            ->remove('id','require|checkArticle');
    }

    /**
     * 详情场景
     * @return ArticleValidate
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }

    /**
     * 更改状态场景
     * @return ArticleValidate
     */
    public function sceneStatus()
    {
        return $this->only(['id', 'status']);
    }

    /**
     * 编辑场景
     * @return ArticleValidate
     */
    public function sceneEdit()
    {
        return $this;
    }

    /**
     * 删除场景
     * @return ArticleValidate
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }

    /**
     * 检查文章是否存在
     * @param $value
     * @return bool|string
     */
    public function checkArticle($value)
    {
        $article = Article::findOrEmpty($value);
        if ($article->isEmpty()) {
            return '文章不存在';
        }
        return true;
    }

}